package org.casper.util;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class CodeServlet
 */
public class CodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		char[] codes = {'a','b','c','d','1','2','3','4','5','6','7','8','9'};
		
		BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		Random random = new Random();
		StringBuffer sb = new StringBuffer("");
		
		for(int i = 0;i<4;i++){
			int index = random.nextInt(codes.length);
			sb.append(codes[index]);
		}
		session.setAttribute("code", sb.toString());
		g.drawString(sb.toString(), 15, 15);
		
		ImageIO.write(image, "png", resp.getOutputStream());
		
		
		
	}
       
	
}
